import config from '/util/config'
function request(args){
    const default_url = config.thisconfig.url;
    // const default_url = 'http://192.168.0.120:8081/'; 
    
    let { url, method, data, methodType } = args;
    let Url = default_url + url;
    url = default_url.concat(url);
    data = data ? data : {};
    methodType = methodType == "application" ? "application/x-www-form-urlencoded" : "application/json;charset=utf-8";
    method = method || 'GET'

    if (method == "POST") {
        // methonType = "application/x-www-form-urlencoded";
    } else if (method == "GET"){
        // GET 请求缓存处理
        let timestamp = new Date().getTime()
        url = url + "?t=" + timestamp
    }

    let userInfo = my.getStorageSync({ key: 'user' });
    let user = userInfo.data ? encodeURIComponent(JSON.stringify(userInfo.data)) : ''
    //验证基础库
    if (my.request) {
        return new Promise((resolve,reject)=>{
            my.request({
                url: url,
                method: method,
                headers: {
                    // "token": encodeURIComponent(JSON.stringify(token)),
                    // "token": token,
                    "user": user,
                    'content-type': methodType
                },
                data: data,
                success: (res) => {
                    console.log("Url======>>",Url,res)
                    console.log("msg======>>",Url,res.data.msg)
                    switch(res.data.code) {
                        case 200:
                            resolve(res.data)
                            break;
                        case 102:
                          my.showToast({content: res.data.msg});

                            reject(res.data)
                            break;
                        case 403:
                            my.showToast({content: res.data.msg});
                            my.removeStorageSync({key: 'user',});
                            reject(res.data)
                            break;
                        default :
                            my.showToast({content: res.data.msg});
                            reject(res.data)
                            break;
                    }
                },
                fail(res) {
                    console.log("===========>>失败",res)
                    my.showToast({
                        centent: res.data.msg,
                        duration: 1000,
                    });
                },
            })
        })
    } 
}
export default request